#include "app_modbus_tcp.h"

#define LOG_TAG              "modbus_tcp"
#include <drv_log.h>

/*************************define******************************************/
lan_sta modbus_sta;
MODBUS_DATA MODBUSTCP_DATA = {0};
#define MODBUS_ETH_NAME "wifi"
/*************************extern******************************************/
extern dev_config dev_config_info;
/*************************rt_thread***************************************/
struct rt_thread modbus_tcp_thread;//modbus tcp
/*************************rt_stack****************************************/
ALIGN(RT_ALIGN_SIZE)
static rt_uint8_t modbus_tcp_thread_stack[2048];//线程栈 

struct rt_semaphore sem_modbustcp_available;

static void thread_modbus_tcp_entry(void *parameter)	
{
	modbus_t *modbustcp = RT_NULL;
	int regs = 0;
	
	rt_thread_delay(10000);
	
	while(1)
	{
		modbus_sta.netdev_modbus = netdev_get_by_name(MODBUS_ETH_NAME);//查找modbus_tcp网卡

		if(modbus_sta.netdev_modbus == RT_NULL)
		{
			//	LOG_E("找不到设备%s\n",MODBUS_ETH_NAME);
		}

		if(netdev_is_link_up(modbus_sta.netdev_modbus))//如果lan口已经连接
		{
			if(modbus_sta.lan_link_ok == RT_FALSE)
			{
			//	LOG_I("LAN已连接\n");
				modbus_sta.lan_link_ok = RT_TRUE;
			}
		}
		else
		{
			modbus_sta.lan_link_ok = 0;
			if(modbus_sta.lan_server_connect_ok == RT_TRUE)
			{
				modbus_close(modbustcp); //7-关闭modbus端口
				modbus_free(modbustcp); //8-释放modbus资源
				modbus_sta.lan_server_connect_ok = RT_FALSE;
			}
			//LOG_E("LAN口断开!\n");
			modbus_sta.lan_sta = LAN_LINK_ERR;
		}

		//通过modbustcp获取服务端数据
		if(modbus_sta.lan_link_ok)
		{
			if(modbus_sta.lan_server_connect_ok == RT_FALSE)
			{	
				modbustcp = modbus_new_tcp(dev_config_info.modbus_tcp.ip,dev_config_info.modbus_tcp.port,AF_INET);//连接服务端
				modbus_set_slave(modbustcp,1);//终端地址
				if(modbus_connect(modbustcp) != RT_EOK)
				{
						modbus_sta.lan_sta = LAN_CONNECT_ERR;
						//LOG_E("连接modbustcp服务端失败\n");
						modbus_close(modbustcp); //7-关闭modbus端口
						modbus_free(modbustcp); //8-释放modbus资源
				}
				else
				{
					//LOG_I("modbustcp服务端链接成功\n");
					modbus_sta.lan_server_connect_ok = RT_TRUE;
					modbus_sta.lan_sta = LAN_NO_ERR;
				}
			}
			if(modbus_sta.lan_server_connect_ok == 1)
			{
				rt_sem_take(&sem_modbustcp_available,RT_WAITING_FOREVER);
				modbus_set_response_timeout(modbustcp, 0, 1000000);
				regs = modbus_read_registers(modbustcp,53600,DC_STA_LEN, MODBUSTCP_DATA.dc_sta);
				if(regs != 3)
				{
					//LOG_E("读取服务端数据失败 起始地址53600 长度:%d\n",DC_STA_LEN);
					modbus_sta.lan_server_connect_ok = RT_FALSE;
					modbus_sta.lan_link_ok = RT_FALSE;
				}
				
				//遥测
				regs = modbus_read_registers(modbustcp,30837,DC_POWER_LEN, MODBUSTCP_DATA.dc_power);
				if(regs != 2)
				{
					//LOG_E("读取服务端数据失败 起始地址30837 长度:%d\n",DC_POWER_LEN);
					modbus_sta.lan_server_connect_ok = RT_FALSE;
					modbus_sta.lan_link_ok = RT_FALSE;
				}

				
				
				if(modbus_sta.lan_link_ok == RT_FALSE)
				{
						modbus_close(modbustcp); //7-关闭modbus端口
						modbus_free(modbustcp); //8-释放modbus资源					
				}
				rt_sem_release(&sem_modbustcp_available);
			}
		}
		rt_thread_delay(5000);
	}
}
/*******************************************************************************
* Function Name  : app_init_modbus_tcp
* Description    : 初始化modbustcp相关
* Input          : void
* Output         : None
* Return         : None
* Attention		   : None
*******************************************************************************/
int app_init_modbus_tcp(void)
{
	
	 rt_sem_init(&sem_modbustcp_available, "modbus_t", 1, RT_IPC_FLAG_PRIO);		
   	//创建静态线程 - modbus_tcp
	rt_thread_init(&modbus_tcp_thread,                		//线程控制块
								 "modbus_tcp",                        			//线程名字，在shell里面可以看到
								 thread_modbus_tcp_entry,           	 	//线程入口函数
								 RT_NULL,                     		  		//线程入口函数参数
								 &modbus_tcp_thread_stack[0],       //线程栈起始地址
								 sizeof(modbus_tcp_thread_stack),   //线程栈大小
								 modbus_tcp_pro,    								    //线程的优先级
								 20);                         		  		//线程时间片
	rt_thread_startup(&modbus_tcp_thread);             	//启动线程 
								 
	return RT_EOK;
}

INIT_APP_EXPORT(app_init_modbus_tcp);


